package com.sl.au.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Account;
import com.sl.au.entity.Module;
import com.sl.au.entity.MyMoney;
import com.sl.au.entity.MyMoneyType;
import com.sl.au.entity.User;
import com.sl.au.service.ContractService;
import com.sl.au.service.MyMoneyService;
import com.sl.au.service.MyMoneyTypeService;
import com.sl.au.service.OptionService;

@RestController
@RequestMapping(value = "/ui/myMoney")
public class MyMoneyController extends BaseController {
	
	@Autowired
	ContractService contractService;
	
	@Autowired
	MyMoneyService myMoneyService;
	
	@Autowired
	MyMoneyTypeService myMoneyTypeService;
	
	@Autowired
	OptionService optionService;
	
	/**
	 * 
	 * 费用管理列表界面
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = this.createLayoutView("myMoney/myMoneyIndex");
		return modelAndView;
	}
	
	/**
	 * 费用管理新增界面
	 * @return
	 */
	@RequestMapping(value = "addInterface")
	public ModelAndView newContract() {
		ModelAndView modelAndView = this.createLayoutView("myMoney/myMoneyAdd");
		Account a = this.getMyAccount();
		String type = "费用管理";
		String superior = a.getUser().getSaleArea().getSuperior();
		List<Object> applyArea = contractService.getAreasBySuperior(superior);
		List<User> allSuperiorMen = contractService.getAllSuperiorMenBySuperior(superior);
		modelAndView.addObject("allSuperiorMen", allSuperiorMen);
		modelAndView.addObject("superior", superior);
		modelAndView.addObject("applyAreas", applyArea);
		modelAndView.addObject("kind", optionService.getRepository().findByGroup(type));
		return modelAndView;
	}
	/**
	 * 费用管理编辑界面
	 * @return
	 */
	@RequestMapping(value = "updateMyMoney")
	public ModelAndView editMyMoney() {
		ModelAndView modelAndView = this.createLayoutView("myMoney/myMoneyEdit");
		String id = request.getParameter("id");
		MyMoney old = myMoneyService.getRepository().findOne(id);
		Account a = this.getMyAccount();
		String superior = a.getUser().getSaleArea().getSuperior();
		String type = "费用管理";
		Date aa = old.getApplyDate();
		Date bb = old.getOutStartDate();
		List<Object> applyArea = contractService.getAreasBySuperior(superior);
		List<User> allSuperiorMen = contractService.getAllSuperiorMenBySuperior(superior);
		modelAndView.addObject("allSuperiorMen", allSuperiorMen);
		modelAndView.addObject("superior", superior);
		modelAndView.addObject("applyDate", old.getApplyDate()!=null ? old.getApplyDate() : "");
		modelAndView.addObject("outStartDate", old.getOutStartDate() != null ? old.getOutStartDate() : "");
		modelAndView.addObject("backDate",old.getBackDate() != null ? old.getBackDate() : "");
		modelAndView.addObject("useContent", old.getUseContent());
		modelAndView.addObject("memo", old.getMemo());
		modelAndView.addObject("applyAreas", applyArea);
		modelAndView.addObject("kind", optionService.getRepository().findByGroup(type));
		modelAndView.addObject("old", old);
		return modelAndView;
	}
	/**
	 * 费用管理查看界面
	 * @return
	 */
	@RequestMapping(value = "viewMyMoney")
	public ModelAndView viewMyMoney() {
		ModelAndView modelAndView = this.createLayoutView("myMoney/myMoneyView");
		String id = request.getParameter("id");
		MyMoney old = myMoneyService.getRepository().findOne(id);
		Account a = this.getMyAccount();
		String superior = a.getUser().getSaleArea().getSuperior();
		List<User> allSuperiorMen = contractService.getAllSuperiorMenBySuperior(superior);
		modelAndView.addObject("allSuperiorMen", allSuperiorMen);
		modelAndView.addObject("superior", superior);
		modelAndView.addObject("applyDate", old.getApplyDate()==null ? "" : old.getApplyDate());
		modelAndView.addObject("outStartDate", old.getOutStartDate() == null ? "" : old.getOutStartDate());
		modelAndView.addObject("backDate",old.getBackDate() == null ? "" : old.getBackDate());
		modelAndView.addObject("useContent", old.getUseContent());
		modelAndView.addObject("memo", old.getMemo());
		modelAndView.addObject("old", old);
		return modelAndView;
	}
	
	/**
	 * 保存我的费用主表
	 * @param request
	 * @param customer
	 * @return
	 */
	@RequestMapping(value = "saveMyMoney", method = RequestMethod.POST)
	public Map<String, Object> saveMyMoney(HttpServletRequest request, @RequestBody MyMoney myMoney) {
		Map<String, Object> map = new HashMap<String, Object>();
		Account a = this.getMyAccount();
		String userName = a.getUser().getName();
		String superior = a.getUser().getSaleArea().getSuperior();
		/*String userId = myMoney.getApplyUser().getId();
		if(userId.equals("") || userId.equals(null)){
			myMoney.setApplyUser(null);
		}*/
		myMoney.setCreatUser(userName);
		myMoney.setCreatDept(superior);
		myMoneyService.getRepository().save(myMoney);
		String id = myMoney.getId();
		map.put("code", 0);
		map.put("id", id);
		return map;
	}
	/**
	 * 保存我的费用明细子表
	 * @param request
	 * @param customer
	 * @return
	 */
	@RequestMapping(value = "updateSaveMyMoneyType", method = RequestMethod.POST)
	public Map<String, Object> updateSaveMyMoneyType(HttpServletRequest request, @RequestBody MyMoneyType myMoneyType) {
		Map<String, Object> map = new HashMap<String, Object>();
		Account a = this.getMyAccount();
		String id = request.getParameter("id");
		MyMoneyType old = myMoneyTypeService.getRepository().findOne(id);
		old.setKind(myMoneyType.getKind());
		old.setMoney(myMoneyType.getMoney());
		myMoneyTypeService.getRepository().save(old);
		map.put("code", 0);
		return map;
	}
	/**
	 * 保存我的费用明细子表
	 * @param request
	 * @param customer
	 * @return
	 */
	@RequestMapping(value = "saveMyMoneyType", method = RequestMethod.POST)
	public Map<String, Object> saveMyMoneyType(HttpServletRequest request, @RequestBody MyMoneyType myMoneyType) {
		Map<String, Object> map = new HashMap<String, Object>();
		Account a = this.getMyAccount();
		myMoneyTypeService.getRepository().save(myMoneyType);
		map.put("code", 0);
		return map;
	}
	/**
	 * 删除费用明细子表
	 * @param request
	 * @param customer
	 * @return
	 */
	@RequestMapping(value = "deleteMyMoneyType", method = RequestMethod.POST)
	public Map<String, Object> deleteMyMoneyType(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		myMoneyTypeService.getRepository().delete(id);
		map.put("code", 0);
		return map;
	}
	
	/**
	 * 删除费用主表
	 * @param request
	 * @param customer
	 * @return
	 */
	@RequestMapping(value = "deleteMyMoney", method = RequestMethod.POST)
	public Map<String, Object> deleteMyMoney(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		myMoneyService.deleteAll(id);
		map.put("code", 0);
		return map;
	}
	/**
	 * 获取我的费用明细列表
	 * @param request
	 * @param customer
	 * @return
	 */
	@RequestMapping(value = "selectMyMoneyType", method = RequestMethod.GET)
	public Map<String, Object> selectMyMoneyType(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		Account a = this.getMyAccount();
		String id = request.getParameter("myMoneyId");
		List<MyMoneyType> list = myMoneyTypeService.getMoneyType(start, length, id);
		long count = myMoneyTypeService.getCount(id);
		map.put("data", list);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	/**
	 * 获取我的费用index列表
	 * @param request
	 * @param customer
	 * @return
	 */
	@RequestMapping(value = "selectMyMoney", method = RequestMethod.GET)
	public Map<String, Object> selectMyMoney(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, String> condition = new HashMap<String, String>();
		Account a = this.getMyAccount();
		String name="myMoney"; 
        Module module = this.getPageModule(name);
        String dataFilter=module.getDataFilter();
        if(dataFilter==null){
        	dataFilter="所有";
        }
        User user = a.getUser();
		String userId=user.getId();
		String superior=user.getSaleArea().getSuperior();
		//String areaId=user.getSaleArea().getId();
		String areaId=user.getSaleArea().getAreaName();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key = request.getParameter("key");
		condition.put("key", key);
		condition.put("dataFilter", dataFilter);
		condition.put("superior", superior);
		condition.put("userId", userId);
		condition.put("areaId", areaId);
		List<MyMoney> list = myMoneyService.getMoney(start, length, condition);
		long count = myMoneyService.getCount(condition);
		map.put("data", list);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	/**
	 * 费用快速录入页面跳转
	 * @return
	 */
	@RequestMapping(value = "addfast")
	public ModelAndView addfast() {
		ModelAndView modelAndView = this.createLayoutView("myMoney/myMoneyAll");
		Account a = this.getMyAccount();
		String userId = a.getUser().getId();
		String userName = a.getUser().getName();
		String superior = a.getUser().getSaleArea().getSuperior();
		List<User> allSuperiorMen = contractService.getAllSuperiorMenBySuperior(superior);
		modelAndView.addObject("allSuperiorMen", allSuperiorMen);
		modelAndView.addObject("superior", superior);
		
		return modelAndView;
	}
	
}
